import org.junit.Test;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.util.ArrayList;
import java.util.HashSet;

public class KeepGoing {


    @Test
    public void test01() throws IOException {
        RandomAccessFile file = new RandomAccessFile(
                "/home/xieweig/document/kafkatest.txt", "rw");

        System.out.println("当前指针位置：　" + file.getFilePointer());
        System.out.println("当前文件总大小　：　" + file.length());
//         byte[] bytes = new byte[128];
//         int length = file.read(bytes);
//         System.out.println(length+"文件内容: "+new String(bytes,0,length));
//         System.out.println("当前指针位置：　"+file.getFilePointer());
//         byte[] bytes2 = new byte[128];
//         int length2 = file.read(bytes2);
//         System.out.println(length2+"文件内容: "+new String(bytes2,0,length2));
//         System.out.println("当前指针位置：　"+file.getFilePointer());
//         byte[] bytes3 = new byte[128];
//         int length3 = file.read(bytes3);
//         System.out.println(length3);
//         System.out.println(length3+"文件内容: "+new String(bytes3,0,length3));
//         System.out.println("当前指针位置：　"+file.getFilePointer());
        StringBuffer stringBuffer = new StringBuffer();
        while (true) {
            byte[] buffer = new byte[128];
            int position = file.read(buffer);
            if (position == -1) break;
            String s = new String(buffer, 0, position);
            stringBuffer.append(s);
            System.out.println(position + "文件内容: " + s);
            System.out.println("当前指针位置：　" + file.getFilePointer());
        }
        System.out.println(stringBuffer.toString());
//         小张今年33岁,��时父母63岁

        file.seek(0);
//         byte[]的容器不是List<byte[]> 也不是StringBuffer 而是bytebuffer
        ByteBuffer buffer = ByteBuffer.allocate((int) file.length() + 100);
        while (true) {
            byte[] b = new byte[128];
            int l = file.read(b);
            if (l == -1) break;
            buffer.put(b, 0, l);
        }

        buffer.flip();
        System.out.println(new String(buffer.array(), buffer.position(), buffer.limit()));
    }

    @Test
    public void test02() throws Exception {
        RandomAccessFile file = new RandomAccessFile(
                "/home/xieweig/document/kafkatest.txt", "rw");

        System.out.println("当前指针位置：　" + file.getFilePointer());
        System.out.println("当前文件总大小　：　" + file.length());

        FileChannel channel = file.getChannel();
        System.out.println("当前通道的信息　：　" + channel.size());
//          FileChannel.open("/home/xieweig/document/kafkatest.txt", OpenOption.);

//          channel.map(FileChannel.MapMode.READ_WRITE,0,channel.size());
    }

    @Test
    public void test03() {

        try (FileChannel open = FileChannel.open(
                Paths.get("/home/xieweig/document/kafkatest.txt")
                , StandardOpenOption.WRITE)) {
            FileLock lock = open.lock();
//               open.position(open.size());
            open.write(ByteBuffer.wrap("你好\n".getBytes()));
            lock.release();
            System.out.println(open.size() + " ?? " + open.position());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void test04() {
        try (FileChannel file = FileChannel.open(Paths.get(
                "/home/xieweig/document/kafkatest.txt"), StandardOpenOption.WRITE)) {
            byte[] ex = "你好\n".getBytes();

            file.position(0);
            ByteBuffer allocate = ByteBuffer.allocate(ex.length);
            allocate.put("占位占位占位".getBytes(), 0, allocate.capacity());
            allocate.flip();
            file.write(allocate);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void test05() {
        try (FileChannel file = FileChannel.open(Paths.get(
                "/home/xieweig/document/kafkatest.txt"),StandardOpenOption.READ, StandardOpenOption.WRITE)) {
            ByteBuffer buffer = ByteBuffer.allocate((int)file.size()+100);
            file.read(buffer);
            System.out.println(buffer);
            buffer.flip();
            System.out.println(buffer);
            byte[] ex = "你好\n".getBytes();
            int length = ex.length;
            System.out.println(ex.length);
            System.out.println( "你好\n".getBytes(StandardCharsets.UTF_8).length);
            buffer.position(length);
            System.out.println(buffer);
            System.out.println(new String(buffer.array(),buffer.position(),buffer.limit()));
            file.position(0);
            file.write(buffer);


        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
